using UnityEngine;

public class DedicatedManager : MonoBehaviour
{
	public PlayerLobby CurrentPlayerLobby;

	public PlayerConnector CurrentPlayer;

	public bool Online = true;

	public string ServerName = "UnitZ BG dedicated server";

	public string ServerPassword = string.Empty;

	public int SceneStartIndex;

	public int MinimumPlayer = 1;

	public int BotNumber = 1;

	public float UpdateTime = 1f;

	public float RestartTime = 5f;

	public float DisconnectTime = 5f;

	public bool AutoRestart = true;

	public GameObject LogCam;

	private float timeTmp;

	private bool isServerRestarting;

	private bool isGameEnded;

	private bool isStarted;

	private GameObject lobbyInstance;

	private void Start()
	{
		UnityEngine.Debug.Log(SystemInfo.deviceUniqueIdentifier);
		StartDedicated();
		AudioListener.pause = true;
	}

	private void findAllPlayers()
	{
		PlayerLobby[] players = UnityEngine.Object.FindObjectsOfType<PlayerLobby>();
		UnitZ.gameNetwork.AssignPlayersToSpawn(players);
	}

	public void StartDedicated()
	{
		if ((bool)lobbyInstance)
		{
			UnityEngine.Object.Destroy(lobbyInstance);
		}
		lobbyInstance = new GameObject("LobbyInstance");
		UnitZ.gameNetwork.IsDedicatedServer = true;
		UnitZ.gameNetwork.matchName = ServerName;
		UnitZ.gameNetwork.HostPassword = ServerPassword;
		UnitZ.gameManager.BotNumber = BotNumber;
		isServerRestarting = false;
		isGameEnded = false;
		isStarted = false;
		if (Online)
		{
			UnitZ.gameNetwork.StartMatchMaker();
			UnitZ.gameNetwork.HostGame(SceneStartIndex, online: true);
			if (ServerLog.instance != null)
			{
				ServerLog.instance.Log("Starting new lobby using Unity Relay Server");
			}
		}
		else
		{
			UnitZ.gameNetwork.HostGame(SceneStartIndex, online: false);
			if (ServerLog.instance != null)
			{
				ServerLog.instance.Log("Starting new lobby on Local network : " + UnitZ.gameNetwork.networkAddress);
			}
		}
	}

	public void OnDisconnect()
	{
		if (base.isActiveAndEnabled)
		{
			UnitZ.gameManager.IsPlaying = false;
			UnityEngine.Debug.Log("Server disconnected, Restarting..");
			isServerRestarting = true;
			timeTmp = Time.time;
			if (ServerLog.instance != null)
			{
				ServerLog.instance.Log("Stopped Host.");
			}
		}
	}

	private void Update()
	{
		if (UnitZ.gameManager.IsPlaying)
		{
			if (CurrentPlayer != null && CurrentPlayer.isServer && UnitZ.NetworkGameplay.IsBattleStart && UnitZ.NetworkGameplay.IsBattleEnded)
			{
				if (!isGameEnded)
				{
					UnityEngine.Debug.Log("Game is over");
					if (ServerLog.instance != null)
					{
						ServerLog.instance.Log("Match is over");
					}
					isGameEnded = true;
					timeTmp = Time.time;
				}
				else if (!isServerRestarting && Time.time >= timeTmp + DisconnectTime)
				{
					UnitZ.gameNetwork.Disconnect();
					UnityEngine.Debug.Log("Stop host!");
					isServerRestarting = true;
					timeTmp = Time.time;
				}
			}
			if (UnitZ.playerManager.Spectre != null)
			{
				UnityEngine.Object.Destroy(UnitZ.playerManager.Spectre.gameObject);
			}
		}
		else if (isServerRestarting)
		{
			if (!(Time.time >= timeTmp + RestartTime))
			{
				return;
			}
			if (AutoRestart)
			{
				UnityEngine.Debug.Log("Restart!");
				StartDedicated();
				isServerRestarting = false;
				if (ServerLog.instance != null)
				{
					ServerLog.instance.Log("Restart hosting..");
				}
			}
			else if (ServerLog.instance != null)
			{
				ServerLog.instance.Log("No restart");
			}
			timeTmp = Time.time;
		}
		else
		{
			if (!(Time.time > timeTmp + UpdateTime))
			{
				return;
			}
			if (CurrentPlayerLobby != null && CurrentPlayerLobby.isServer)
			{
				int lobbyPlayerCount = UnitZ.gameNetwork.GetLobbyPlayerCount();
				if (lobbyPlayerCount - 1 >= MinimumPlayer && UnitZ.gameNetwork.IsOnLobby(string.Empty) && !isStarted)
				{
					UnityEngine.Debug.Log("Start Server");
					findAllPlayers();
					isStarted = UnitZ.gameNetwork.StartServerGame();
				}
			}
			timeTmp = Time.time;
		}
	}

	public void ForceStartServer()
	{
		if (ServerLog.instance != null)
		{
			ServerLog.instance.Log("Force start match");
		}
		StartCoroutine(UnitZ.gameNetwork.ServerCountdownCoroutine());
	}
}
